/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sqlserver;

import java.awt.Component;
import javax.swing.ImageIcon;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;

/**
 *
 * @author PHAMDANH
 */
public class MyTreeCellRenderer extends DefaultTreeCellRenderer
{
    ImageIcon icon_database = new ImageIcon(getClass().getResource("/images/database.png"));
    ImageIcon icon_table = new ImageIcon(getClass().getResource("/images/table.png"));
    ImageIcon icon_computer = new ImageIcon(getClass().getResource("/images/computer.png"));
    ImageIcon icon_folder = new ImageIcon(getClass().getResource("/images/folder.png"));
    ImageIcon icon_user = new ImageIcon(getClass().getResource("/images/user.png"));
    ImageIcon icon_fk = new ImageIcon(getClass().getResource("/images/foreign_key.png"));
    ImageIcon icon_pk = new ImageIcon(getClass().getResource("/images/primary_key.png"));
    public MyTreeCellRenderer()
    {

    }

    @Override
    public Component getTreeCellRendererComponent( JTree tree, Object value, boolean selected, boolean expanded,
                                                    boolean leaf, int row, boolean hasFocus) 
    {            
        super.getTreeCellRendererComponent( tree, value, selected, expanded, leaf, row, hasFocus);            
        DefaultMutableTreeNode tempNode = (DefaultMutableTreeNode) value;  
        int level = tempNode.getLevel();
        int idx = 0;
        if(level==2)
        {
            DefaultMutableTreeNode parent = (DefaultMutableTreeNode)tempNode.getParent();
            DefaultMutableTreeNode root = (DefaultMutableTreeNode)tempNode.getRoot();
            idx=root.getIndex(parent);
        }
        switch(level)
        {
            case 0:                    
                setIcon(icon_computer);
                break;
            case 1:
                setIcon(icon_folder);
                break;
            case 2:
            {
                if(idx==0)
                    setIcon(icon_database);
                 else
                    setIcon(icon_user);
            }
                break;
            case 3:
                setIcon(icon_folder);
                break;
            case 4:
                setIcon(icon_table);
                break;
            case 5:
                setIcon(icon_folder);
                break;
            case 6:
            {
                String name = tempNode.getUserObject().toString();
                if(name.charAt(0)=='p')
                    setIcon(icon_pk);
                else
                    setIcon(icon_fk);
            }
                break;
            default:
                break;
        }       
        return this;
    }
    
}
